// import com.alibaba.druid.sql.ast.SQLExpr;
// import com.alibaba.druid.sql.ast.SQLPartitionBy;
// import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject;
// import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

#include "MySqlPartitionByKey.h"
#include "../../../../../sql/ast/SQLPartition.h"
#include "../../../../../utils/instanceof.h"
#include "../../../../../utils/log.h"
#include "../../../../../sql/ast/SQLExpr.h"
#include "../../../../../sql/visitor/SQLASTVisitor.h"
#include "../../../../../sql/dialect/mysql/visitor/MySqlASTVisitor.h"
#include "../../../../../Exception/IllegalArgumentException.h"
#include "../../../../../utils/listTran.h"
#include "../../../../../utils/log.h"
#include "../../../../../sql/ast/SQLSubPartitionBy.h"

MySqlPartitionByKey::MySqlPartitionByKey()
{
  algorithm = 2;
}

// @Override
void MySqlPartitionByKey::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, MySqlASTVisitor>(visitor.get()))
  {
    MySqlASTVisitor_ptr tmp = std::dynamic_pointer_cast<MySqlASTVisitor>(visitor);
    accept0(tmp);
  }
  else
  {
    throw new IllegalArgumentException(make_string_ptr("not support visitor type : " + *visitor->getClass()));
  }
}

// @Override
void MySqlPartitionByKey::accept0(MySqlASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(MySqlPartitionByKey)))
  {
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLExpr, SQLObject>(columns);
    acceptChild(visitor, tmp1);
    acceptChild(visitor, partitionsCount);
    SQLObject_list_ptr tmp2 = ListTran::tran_class<SQLPartition, SQLObject>(getPartitions());
    acceptChild(visitor, tmp2);
    acceptChild(visitor, subPartitionBy);
  }
  visitor->endVisit(SharedObject(MySqlPartitionByKey));
}

void MySqlPartitionByKey::cloneTo(MySqlPartitionByKey_ptr x)
{
  SQLPartitionBy::cloneTo(x);
  for (SQLExpr_ptr column : *columns)
  {
    SQLExpr_ptr c2 = std::dynamic_pointer_cast<SQLExpr>(column->clone());
    c2->setParent(x);
    x->columns->push_back(c2);
  }
  x->algorithm = algorithm;
}

SQLObject_ptr MySqlPartitionByKey::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<MySqlPartitionByKey>(new MySqlPartitionByKey());
  std::shared_ptr<MySqlPartitionByKey> x = std::dynamic_pointer_cast<MySqlPartitionByKey>(clone_tmp);
  // MySqlPartitionByKey *x = new MySqlPartitionByKey();
  cloneTo(x);
  return x;
}

int MySqlPartitionByKey::getAlgorithm()
{
  return algorithm;
}

void MySqlPartitionByKey::setAlgorithm(int algorithm)
{
  this->algorithm = algorithm;
}
